﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BlackBird.Models;
using System.Web.Mvc;

namespace BlackBird.DataAccess
{
	public class ContractDataAccess
	{
		/// <summary>
		/// 保存实例合同
		/// </summary>
		/// <param name="FuJianID"></param>
		/// <param name="UserID"></param>
		/// <param name="Name"></param>
		/// <returns></returns>
		public static int NewSampleHeTong(int FuJianID,string UserID, string Name)
		{
			using (var context = new blackbirdEntities())
			{
				var obj = new samplehetong { Name = Name, Date = DateTime.Now, UserID = UserID };
				context.samplehetong.AddObject(obj);
				context.SaveChanges();
				var heTongID = obj.idSampleHeTong;

				var hetong_fujian = new samplehetong_fujian { FuJianID = FuJianID, SampleHeTongID = heTongID };
				context.samplehetong_fujian.AddObject(hetong_fujian);
				context.SaveChanges();

				return heTongID;
			}
		}

		/// <summary>
		/// 保存项目相关的合同信息
		/// </summary>
		/// <param name="FuJianID"></param>
		/// <param name="UserID"></param>
		/// <param name="Name"></param>
		/// <returns></returns>
		public static int NewProjectHeTong(int ProjectId,int FuJianID, string UserID, string Name)
		{
			using (var context = new blackbirdEntities())
			{
				var obj = new projecthetong { ProjectID = ProjectId, Name = Name, CreateDate = DateTime.Now, UserID = UserID };
				context.projecthetong.AddObject(obj);
				context.SaveChanges();
				var heTongID = obj.idProjectHeTong;

				var hetong_fujian = new projecthetong_fujian { FuJianId = FuJianID, ProjectHeTongId = heTongID };
				context.projecthetong_fujian.AddObject(hetong_fujian);
				context.SaveChanges();
				return heTongID;
			}
		}


		/// <summary>
		/// Get all Sample HeTong
		/// </summary>
		/// <param name="ctrl"></param>
		/// <returns></returns>
		public static List<AttachedFile> GetAllSampleHeTongs(Controller ctrl)
		{
			List<AttachedFile> attachedFiles = new List<AttachedFile>();

			List<fujian> fujians = new List<fujian>();

			List<int> idlist = null;
			using (var context = new blackbirdEntities())
			{
				var ids = context.samplehetong.Select<samplehetong, int>(v => v.idSampleHeTong);

				idlist = ids.ToList();
			}

			using (var context = new blackbirdEntities())
			{
				foreach (var id in idlist)
				{
					var heton_fujian = context.samplehetong_fujian.First(v => v.SampleHeTongID == id);
					if (heton_fujian == null)
						throw new NullReferenceException("Not found expected connection from sample he tong -- fujian");

					var fujian = context.fujian.FirstOrDefault(v => v.idFujian == heton_fujian.FuJianID);

					if (fujian == null)
						throw new NullReferenceException("Not found expected fujian");

					fujians.Add(fujian);
				}

			}

			foreach (var fujian in fujians)
			{
				AttachedFile af = BuildAttachedFileFromFujian(fujian, ctrl);
				attachedFiles.Add(af);
			}


			return attachedFiles;
		}

		/// <summary>
		/// 返回项目相关的合同文档。
		/// </summary>
		/// <param name="ProjectId"></param>
		/// <param name="ctrl"></param>
		/// <returns></returns>
		public static List<AttachedFile> GetProjectRelatedHeTongs(int ProjectId, Controller ctrl)
		{
			List<AttachedFile> attachedFiles = new List<AttachedFile>();

			List<fujian> fujians = new List<fujian>();

			List<projecthetong> hetonglist = null;
			using (var context = new blackbirdEntities())
			{
				var hetongs = from h in context.projecthetong
							  where h.ProjectID == ProjectId
							  select h;

				hetonglist = hetongs.ToList();
			}

			using (var context = new blackbirdEntities())
			{
				foreach (var hetong in hetonglist)
				{
					var heton_fujian = context.projecthetong_fujian.First(v => v.ProjectHeTongId == hetong.idProjectHeTong);
					if (heton_fujian == null)
						throw new NullReferenceException("Not found expected connection from sample he tong -- fujian");

					var fujian = context.fujian.FirstOrDefault(v => v.idFujian == heton_fujian.FuJianId);

					if (fujian == null)
						throw new NullReferenceException("Not found expected fujian");

					AttachedFile af = BuildAttachedFileFromFujian(fujian, ctrl);
					attachedFiles.Add(af);					
				}
			}

			return attachedFiles;
		}

		public static AttachedFile BuildAttachedFileFromFujian(fujian file,Controller ctrl)
		{
			AttachedFile af = new AttachedFile();
			af.FileName = file.Name;
			af.OwnerId = file.User_idUser;
			af.UploadDate = ((DateTime)(file.Date)).ToString("yyyy-MM-dd");
			af.UploadUserName = AccountDataAccess.GetUserInfoByUserID(file.User_idUser).DisplayName;
			
			af.DownloadUrl = ctrl.Url.Action("Download","FileUpload",new { id = file.idFujian.ToString()});

			return af;
		}
	}
}